<?php
/**
 * 作者：New一个
 * 主页：http://www.newyige.com
 * 描述：主题选项类
 * 时间：2016.05.16
 */

//~ 读取选项设置 echo new_option("ID名称","默认输出值");
if (!function_exists('new_option')) {
	function new_option($name, $default = false) {
		$options = get_option(new_key);
		if (isset($options[$name]))
			return $options[$name];
		return $default;
	}

}

//~ 设置选项类
class OptionsClass {

	var $options, $menuinfo;

	//~ 构造函数
	function __construct($options, $menuinfo) {
		$this -> options = $options;
		$this -> menuinfo = $menuinfo;
		//~ 创建设置页面
		$this -> make_data_available();
		add_action('admin_menu', array(&$this, 'add_admin_menu'));
		//~ 执行加载js和css的函数
		if (isset($_GET['page']) && ($_GET['page'] == $this -> menuinfo['urlname'])) {
			add_action('admin_enqueue_scripts', array(&$this, 'enqueue_css_js'));
		}
	}

	//~ 创建后台菜单
	function add_admin_menu() {
		if ($this -> menuinfo['submenu']) {
			$parentname = $this -> menuinfo['parentname'];
			add_submenu_page($parentname, $this -> menuinfo['menuname'], $this -> menuinfo['menuname'], 'manage_options', $this -> menuinfo['urlname'], array(&$this, 'initialize'));
		} else {
			add_menu_page($this -> menuinfo['menuname'], $this -> menuinfo['menuname'], 'manage_options', $this -> menuinfo['urlname'], array(&$this, 'initialize'), '', 59);
		}
	}

	//~ 调用js、css
	public function enqueue_css_js() {
		wp_enqueue_media();
		wp_enqueue_style('wp-color-picker');
		wp_enqueue_script('wp-color-picker');
		wp_enqueue_script('jquery-ui-tabs');
		wp_enqueue_style('options_css', get_template_directory_uri() . '/options/options.css');
		wp_enqueue_script('options_js', get_template_directory_uri() . '/options/options.js', '', '', true);
		wp_localize_script('options_js', 'upload_preview', array('img_base' => includes_url(), 'img_path' => array('archive' => 'images/media/archive.png', 'audio' => 'images/media/audio.png', 'code' => 'images/media/code.png', '_default' => 'images/media/default.png', '_document' => 'images/media/document.png', 'interactive' => 'images/media/interactive.png', 'spreadsheet' => 'images/media/spreadsheet.png', '_text' => 'images/media/text.png', 'video' => 'images/media/video.png'), 'ajaxurl' => admin_url('admin-ajax.php')));
	}

	//~ 初始化
	function initialize() {
		$this -> get_save_options();
		$this -> make_data_available();
		$this -> display();
	}

	//~ 更新数据
	function get_save_options() {
		$oldoption = $newoptions = get_option(new_key);
		if (isset($_REQUEST['OptionsClass']) && check_admin_referer('options_action', 'OptionsClass')) {
			echo '<div class="updated notice is-dismissible"><p><strong>保存成功</strong></p><button type="button" class="notice-dismiss"></button></div>';
			foreach ($this->options as $option) {
				if (in_array($option['type'], array('text', 'textarea'))) {
					$value = stripslashes($_POST[$option['id']]);
					$newoptions[$option['id']] = $value;
				} elseif ($option['type'] == 'checkbox') {
					$newoptions[$option['id']] = empty($_POST[$option['id']]) ? array() : $_POST[$option['id']];
				} elseif ($option['type'] == 'checkbox1') {
					$newoptions[$option['id']] = empty($_POST[$option['id']]) ? array() : $_POST[$option['id']];
				} elseif ($option['type'] == 'arrays') {
					$value = explode(',', $_POST[$option['id']]);
					$value = array_filter($value);
					$newoptions[$option['id']] = $value;
				} elseif (in_array($option['type'], array('open', 'close', 'title'))) {
					continue;
				} else {
					$value = $_POST[$option['id']];
					$newoptions[$option['id']] = $value;
				}
			}
		}
		if ($oldoption != $newoptions) {
			$oldoption = $newoptions;
			update_option(new_key, $oldoption);
		}
		$options[new_key] = $oldoption;
	}

	//~ 设置选项数据
	function make_data_available() {
		global $options;
		$options[new_key] = get_option(new_key);
		$option_conf = $this -> options;
		foreach ($option_conf as $key => $option) {
			if (isset($option['id']) && isset($options[new_key][$option['id']])) {
				$this -> options[$key]['std'] = $options[new_key][$option['id']];
			}
		}
	}

	//~ 页面输出
	function display() {
		$saveoption = false;
		echo '<div class="wrap">';
		echo '<h1>' . $this -> menuinfo['menuname'] . '</h1>';
		echo '<form method="post" action="" autocomplete="off">';
		echo '<div class="options_tabs">';
		$this -> tab_toggle($this -> options);
		echo '<div class="options_cnt">';
		//~ 根据选项类型执行对应函数
		foreach ($this->options as $option) {
			if (($option['type'] == 'open' || $option['type'] == 'close' || $option['type'] == 'title') || (isset($option['id']) && method_exists($this, $option['type']))) {
				if (!isset($option['std']))
					$option['std'] = '';
				if (in_array($option['type'], array('text', 'textarea', )))
					$option['std'] = htmlspecialchars($option['std']);
				$this -> {$option['type']}($option);
				$saveoption = true;
			}
		}
		echo '</div>';
		if ($saveoption) {
			echo '<div class="save_options">';
			wp_nonce_field('options_action', 'OptionsClass');
			echo '<input class="button-primary" type="submit" name="Submit" value="保存设置" /></div>';
		}
		echo '</div></form></div>';
	}

	//~ 获取页面列表
	public function get_pages_by_level($args, $space = '') {
		$pages = array();
		$top_pages = get_pages($args);
		if (!empty($top_pages)) {
			foreach ($top_pages as $page) {
				$pages[$page -> ID] = $page -> post_title;
				$args['parent'] = $page -> ID;
				$submenu_pages = $this -> get_pages_by_level($args);
				foreach ($submenu_pages as $key => $title) {
					$pages[$key] = $space . $title;
				}
			}
		}
		return $pages;
	}

	//~ 获取分类列表
	public function get_terms_by_level($args, $space = '') {
		$terms = array();
		$top_terms = get_terms($args);
		if (!empty($top_terms)) {
			foreach ($top_terms as $term) {
				$terms[$term -> term_id] = $term -> name;
				$args['parent'] = $term -> term_id;
				$submenu_terms = $this -> get_terms_by_level($args);
				foreach ($submenu_terms as $key => $title) {
					$terms[$key] = $space . $title;
				}
			}
		}
		return $terms;
	}

	//~ 选项卡切换
	public function tab_toggle($arr) {
		if (!$arr)
			return;
		$output = '';
		foreach ($arr as $values) {
			if (!isset($values['id']))
				continue;
			if ($values['type'] == 'open') {
				if (!isset($values['name']) || $values['name'] == '')
					$values['name'] = $values['id'];
				$output .= '<li><a href="#' . $values['id'] . '" data-toggle="tab">' . $values['name'] . '</a></li>';
			}
		}
		if ($output != '')
			echo '<ul class="tabs_nav">' . $output . '</ul>';
	}

	//~ 选项卡打开 type="open"
	public function open($values) {
		if (!isset($values['id']))
			return;
		echo '<div id="' . $values['id'] . '" class="tabs_box">';
	}

	//~ 选项卡关闭 type="close"
	public function close($values) {
		echo '</div>';
	}

	//~ 标题 type="title"
	public function title($values) {
		if (isset($values['name']))
			echo '<h3 class="title">' . $values['name'] . '</h3>';
	}

	//~ 文本框 type="text"
	public function text($values) {
		if (!isset($values['id']))
			return;
		if (!isset($values['std']))
			$values['std'] = '';
		echo '<div class="row">';
		echo '<div class="left">';
		if (isset($values['name']))
			echo $values['name'];
		echo '</div>';
		echo '<div class="center"><input type="text" value="' . $values['std'] . '" class="options_input" id="' . $values['id'] . '" name="' . $values['id'] . '"/></div>';
		if (isset($values['desc']) && $values['desc'] != '')
			echo '<div class="right"><p>' . $values['desc'] . '</p></div>';
		echo '</div>';
	}

	//~ 文本域 type="textarea"
	public function textarea($values) {
		if (!isset($values['id']))
			return;
		if (!isset($values['std']))
			$values['std'] = '';
		echo '<div class="row">';
		echo '<div class="left">';
		if (isset($values['name']))
			echo $values['name'];
		echo '</div>';
		echo '<div class="center"><textarea rows="3" class="options_textarea" id="' . $values['id'] . '" name="' . $values['id'] . '">' . $values['std'] . '</textarea></div>';
		if (isset($values['desc']) && $values['desc'] != '')
			echo '<div class="right"><p>' . $values['desc'] . '</p></div>';
		echo '</div>';
	}

	//~ 颜色 type="color"
	public function color($values) {
		if (!isset($values['id']))
			return;
		if (!isset($values['std']))
			$values['std'] = '';
		echo '<div class="row">';
		echo '<div class="left">';
		if (isset($values['name']))
			echo $values['name'];
		echo '</div>';
		echo '<div class="center"><input type="text" value="' . $values['std'] . '" class="color_picker options_input" id="' . $values['id'] . '" name="' . $values['id'] . '"/></div>';
		if (isset($values['desc']) && $values['desc'] != '')
			echo '<div class="right"><p>' . $values['desc'] . '</p></div>';
		echo '</div>';
	}

	//~ 单选按钮 type="radio"
	public function radio($values) {
		if (!isset($values['id']))
			return;
		if (!isset($values['std']))
			$values['std'] = '';
		echo '<div class="row">';
		echo '<div class="left">';
		if (isset($values['name']))
			echo $values['name'];
		echo '</div>';
		echo '<div class="center">';
		if (empty($values['sub']))
			$values['sub'] = '';
		if ($values['sub'] == 'page') {
			$entries = $this -> get_pages_by_level(array('parent' => 0), '');
		} elseif ($values['sub'] == 'category') {
			$entries = $this -> get_terms_by_level(array('taxonomy' => $values['sub'], 'hide_empty' => false, 'parent' => 0), '');
		} else {
			if (!is_array($values['sub']))
				$values['sub'] = array();
			$entries = $values['sub'];
		}
		foreach ($entries as $key => $entry) {
			$id = $key;
			$title = $entry;
			$checked = '';
			if ($values['std'] == $id)
				$checked = 'checked = "checked"';
			echo '<label><input ' . $checked . ' type="radio" class="options_radio" value="' . $id . '" id="' . $values['id'] . '_' . $id . '" name="' . $values['id'] . '"/>' . $title . '</label>';
		}
		echo '</div>';
		if (isset($values['desc']) && $values['desc'] != '')
			echo '<div class="right"><p>' . $values['desc'] . '</p></div>';
		echo '</div>';
	}

	//~ 复选框 type="checkbox"
	public function checkbox($values) {
		if (!isset($values['id']))
			return;
		if (!isset($values['std']) || !is_array($values['std']))
			$values['std'] = array();
		echo '<div class="row">';
		echo '<div class="left">';
		if (isset($values['name']))
			echo $values['name'];
		echo '</div>';
		echo '<div class="center">';
		if (empty($values['sub']))
			$values['sub'] = '';
		if ($values['sub'] == 'page') {
			$entries = $this -> get_pages_by_level(array('parent' => 0), '');
		} elseif ($values['sub'] == 'category') {
			$entries = $this -> get_terms_by_level(array('taxonomy' => $values['sub'], 'hide_empty' => false, 'parent' => 0), '');
		} else {
			if (!is_array($values['sub']))
				$values['sub'] = array();
			$entries = $values['sub'];
		}
		foreach ($entries as $key => $entry) {
			$id = $key;
			$title = $entry;
			$checked = "";
			if (in_array($id, $values['std'])) {
				$checked = 'checked = "checked"';
			}
			echo '<label><input ' . $checked . ' type="checkbox" class="options_checkbox" value="' . $id . '" id="' . $values['id'] . '_' . $id . '" name="' . $values['id'] . '[]"/>' . $title . '</label>';
		}
		echo '</div>';
		if (isset($values['desc']) && $values['desc'] != '')
			echo '<div class="right"><p>' . $values['desc'] . '</p></div>';
		echo '</div>';
	}

	//~ 下拉列表 type="select"
	public function select($values) {
		if (!isset($values['id']))
			return;
		if (!isset($values['std']))
			$values['std'] = '';
		if (empty($values['sub']))
			$values['sub'] = '';
		if ($values['sub'] == 'page') {
			$select = '未选择页面';
			$entries = $this -> get_pages_by_level(array('parent' => 0), '');
		} elseif ($values['sub'] == 'category') {
			$select = '未选择分类';
			$entries = $this -> get_terms_by_level(array('taxonomy' => $values['sub'], 'hide_empty' => false, 'parent' => 0), '&nbsp;-&nbsp;');
		} else {
			if (!is_array($values['sub']))
				$values['sub'] = array();
			$select = '未选择';
			$entries = $values['sub'];
		}
		echo '<div class="row">';
		echo '<div class="left">';
		if (isset($values['name']))
			echo $values['name'];
		echo '</div>';
		echo '<div class="center">';
		echo '<select class="options_select" id="' . $values['id'] . '" name="' . $values['id'] . '">';
		echo '<option value="">' . $select . '</option>';
		foreach ($entries as $key => $entry) {
			$id = $key;
			$title = $entry;
			if ($values['std'] == $id) {
				$selected = "selected='selected'";
			} else {
				$selected = "";
			}
			echo '<option ' . $selected . ' value="' . $id . '">' . $title . '</option>';
		}
		echo '</select>';
		echo '</div>';
		if (isset($values['desc']) && $values['desc'] != '')
			echo '<div class="right"><p>' . $values['desc'] . '</p></div>';
		echo '</div>';
	}

	//~ 上传 type="upload"
	public function upload($values) {
		if (!isset($values['id']))
			return;
		if (!isset($values['std']))
			$values['std'] = '';
		$button = (isset($values['button'])) ? $values['button'] : '上传';
		$file_view = '';
		if ($values['std'] != '') {
			$file_type = substr($values['std'], strrpos($values['std'], '.') + 1);
			if (in_array($file_type, array('png', 'jpg', 'gif', 'ico', 'bmp'))) {
				$file_view = '<img src="' . $values['std'] . '" />';
			} elseif (in_array($file_type, array('zip', 'rar', '7z', 'gz', 'tar', 'bz', 'bz2'))) {
				$file_view = '<img src="' . includes_url() . $this -> file_png['archive'] . '" />';
			} elseif (in_array($file_type, array('mp3', 'wma', 'wav', 'mod', 'ogg', 'au'))) {
				$file_view = '<img src="' . includes_url() . $this -> file_png['audio'] . '" />';
			} elseif (in_array($file_type, array('avi', 'mov', 'wmv', 'mp4', 'flv', 'mkv'))) {
				$file_view = '<img src="' . includes_url() . $this -> file_png['video'] . '" />';
			} elseif (in_array($file_type, array('swf'))) {
				$file_view = '<img src="' . includes_url() . $this -> file_png['interactive'] . '" />';
			} elseif (in_array($file_type, array('php', 'js', 'css', 'json', 'html', 'xml'))) {
				$file_view = '<img src="' . includes_url() . $this -> file_png['code'] . '" />';
			} elseif (in_array($file_type, array('doc', 'docx', 'pdf', 'wps'))) {
				$file_view = '<img src="' . includes_url() . $this -> file_png['_document'] . '" />';
			} elseif (in_array($file_type, array('xls', 'xlsx', 'csv', 'et', 'ett'))) {
				$file_view = '<img src="' . includes_url() . $this -> file_png['spreadsheet'] . '" />';
			} elseif (in_array($file_type, array('txt', 'rtf'))) {
				$file_view = '<img src="' . includes_url() . $this -> file_png['_text'] . '" />';
			} else {
				$file_view = '<img src="' . includes_url() . $this -> file_png['_default'] . '" />';
			}
		}
		echo '<div class="row">';
		echo '<div class="left">';
		if (isset($values['name']))
			echo $values['name'];
		echo '</div>';
		echo '<div class="center">';
		echo '<input type="text" class="upload_images" value="' . $values['std'] . '" name="' . $values['id'] . '" id="' . $values['id'] . '_upload"/><a id="' . $values['id'] . '" class="upload_button button" href="javascript:void(0);">' . $button . '</a><a href="javascript:void(0);" class="upload_delete button">清除</a>';
		echo '<div class="upload_preview" id="' . $values['id'] . '_preview">' . $file_view . '</div>';
		echo '</div>';
		if (isset($values['desc']) && $values['desc'] != '')
			echo '<div class="right"><p>' . $values['desc'] . '</p></div>';
		echo '</div>';
	}

}
